package com.nerve.android.cloudoffice.app.activity.assist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.data.DepartmentData;

@Acvitity(layout=R.layout.assist_select_header)
public class HeaderSelectActivity extends NavigationActivity implements OnClickListener{

	public static final String MALE = "male";
	public static final String HEADER = "header";
	
	private List<String> nameList = new ArrayList<String>();
	private boolean isMale;
	
	@ViewOnId(id=R.id.headerGrid)
	private GridView gridView;
	
	private HeaderAdapter adapter;
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("选择头像");
		
		adapter = new HeaderAdapter();
		gridView.setAdapter(adapter);
	}
	
	@Override
	protected void initData() {
		isMale = getIntent().getBooleanExtra(MALE, true);
		
		Iterator<String> it = 
				isMale?
				DepartmentData.MALE_HEADER.keySet().iterator()
				:
				DepartmentData.FEMALE_HEADER.keySet().iterator();
				
		while(it.hasNext()){
			nameList.add(it.next());
		}
	}
	

	@Override
	public void onClick(View v) {
		int index = v.getId();
		
		getIntent().putExtra(HEADER, nameList.get(index));
		setResult(RESULT_OK, getIntent());
		finish();
	}
	
	private class HeaderAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return nameList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return nameList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View v, ViewGroup arg2) {
			if(v == null)
				v = View.inflate(HeaderSelectActivity.this, R.layout.assist_select_header_item, null);
			
			ImageView iv = (ImageView)v;
			iv.setImageResource(
					DepartmentData.getHeaderResource(isMale? User.MALE:User.FEMALE, nameList.get(arg0))
					);
			
			iv.setOnClickListener(HeaderSelectActivity.this);
			iv.setId(arg0);
			return v;
		}
	}
}
